package com.cyt.work.site.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.site.dao.NoticeDao;
import com.cyt.work.site.dao.NoticeRecordDao;
import com.cyt.work.site.model.po.Notice;
import com.cyt.work.site.model.po.NoticeRecord;
import com.cyt.work.site.model.vo.NoticeRecordVo;
import com.cyt.work.site.service.NoticeRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 微官网-校园资讯阅读记录Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-9 18:13:41
 */
@Service("noticeRecordService")
public class NoticeRecordServiceImpl extends BaseServiceImpl<NoticeRecord> implements NoticeRecordService {

    @Resource
    private NoticeRecordDao noticeRecordDao;
    @Resource
    private NoticeDao noticeDao;

    @Autowired
    public void setBaseDao(NoticeRecordDao noticeRecordDao) {
        this.baseDao = noticeRecordDao;
    }

    /**
     * 分页查询
     *
     * @param noticeRecordVo 接受参数对象
     * @param pageNum        分页数
     * @param pageSize       每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    public ReturnResult findPage(NoticeRecordVo noticeRecordVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(NoticeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        List<NoticeRecord> list = noticeRecordDao.selectByExample(example);
        PageInfo<NoticeRecord> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param noticeRecordVo 接受参数对象
     * @param pageNum        分页数
     * @param pageSize       每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    public ReturnResult findVoPage(NoticeRecordVo noticeRecordVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<NoticeRecordVo> list = noticeRecordDao.selectVoList(noticeRecordVo);
        PageInfo<NoticeRecordVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param noticeRecordVo 接受参数对象
     * @return List<NoticeRecord>
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    public List<NoticeRecord> findList(NoticeRecordVo noticeRecordVo) {
        Example example = new Example(NoticeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        return noticeRecordDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param noticeRecordVo 接受参数对象
     * @return List<NoticeRecord>
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    public List<NoticeRecordVo> findVoList(NoticeRecordVo noticeRecordVo) {
        return noticeRecordDao.selectVoList(noticeRecordVo);
    }

    /**
     * 单个对象查询
     *
     * @param noticeRecordVo 接受参数对象
     * @return NoticeRecord
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    public NoticeRecord findOne(NoticeRecordVo noticeRecordVo) {
        NoticeRecord noticeRecord = new NoticeRecord();
        if (StrUtil.isNotEmpty(noticeRecordVo.getId())) {
            noticeRecord.setId(noticeRecordVo.getId());
        } else {
            noticeRecord.setNoticeId(noticeRecordVo.getNoticeId());
            noticeRecord.setMemberId(noticeRecordVo.getMemberId());
        }
        return noticeRecordDao.selectOne(noticeRecord);
    }

    /**
     * 单个对象查询
     *
     * @param noticeRecordVo 接受参数对象
     * @return NoticeRecord
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    public NoticeRecordVo findVoOne(NoticeRecordVo noticeRecordVo) {
        return noticeRecordDao.selectVoOne(noticeRecordVo);
    }

    /**
     * 添加或更新
     *
     * @param noticeRecordVo 参数接受对象
     * @param sysUser        当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(NoticeRecordVo noticeRecordVo, SysUser sysUser) {
        NoticeRecord noticeRecord;
        if (sysUser == null) {
            noticeRecord = new NoticeRecord();
            noticeRecord.setId(BaseUtil.uId());
            noticeRecord.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
            noticeRecord.setCreateTime(new Date());
            noticeRecord.setUpdateTime(new Date());
        } else {
            noticeRecord = new NoticeRecord(sysUser);
        }
        noticeRecord.setNoticeId(noticeRecordVo.getNoticeId());
        noticeRecord.setMemberId(noticeRecordVo.getMemberId());
        noticeRecordDao.insert(noticeRecord);
        //修改阅读次数
        Notice notice = noticeDao.selectByPrimaryKey(noticeRecordVo.getNoticeId());
        if (notice != null) {
            notice.setReadNum(notice.getReadNum() + 1);
            noticeDao.updateByPrimaryKey(notice);
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
    }

    /**
     * 根据条件逻辑删除
     *
     * @param noticeRecordVo 参数接受对象
     * @param sysUser        当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(NoticeRecordVo noticeRecordVo, SysUser sysUser) {
        //修改字段
        NoticeRecord noticeRecord = new NoticeRecord();
        noticeRecord.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        noticeRecord.setUpdateUser(sysUser.getId());
        noticeRecord.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(NoticeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(noticeRecordVo.getId())) {
            criteria.andIn("id", Arrays.asList(noticeRecordVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        noticeRecordDao.updateByExampleSelective(noticeRecord, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param noticeRecordVo 参数接受对象
     * @param sysUser        当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-9 18:13:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(NoticeRecordVo noticeRecordVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(NoticeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(noticeRecordVo.getId())) {
            criteria.andIn("id", Arrays.asList(noticeRecordVo.getId().split(",")));
        }
        noticeRecordDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}